/**
 * 项目名称: Screeps Arena code
 * 创建时间: 2023/9/27
 * 作者: 码力全开
 */
import {BodyPartConstant} from "game/constants";
import {Creep, StructureContainer, StructureSpawn, StructureWall} from "game/prototypes";
import {getObjectsByPrototype} from "game/utils";
import {army, tempT} from "../types/army.mjs";

export default class Room {
    get check_count(): number {
        return this._check_count;
    }

    set check_count(value: number) {
        this._check_count = value;
    }
    /**
     * 我的基地
     * @private
     */
    private _MY_Structure_Spawn:StructureSpawn | null = null;
    get MY_Structure_Spawn(): StructureSpawn | null  {
        return this._MY_Structure_Spawn;
    }

    set MY_Structure_Spawn(value: StructureSpawn ) {
        this._MY_Structure_Spawn = value;
    }
    get energy(): number {
        return this._energy;
    }
    /**
     * 基地能量值
     * @private
     */
    private _energy : number = 0
    /**
     * 游戏进度
     * @private
     */
    private _check_count: number = 0

    /**
     * 创建工种
     * @param type 工种类型
     * @constructor
     */
    public CreateCreep(type:BodyPartConstant[]){
        if (!this.MY_Structure_Spawn) {
            this.FindFort()
            return false
        }
        return  this.MY_Structure_Spawn?.spawnCreep(type)
    }
    /**
     * 找到基地对象
     * @constructor
     * @private
     */
    public FindFort(){
        const result =  getObjectsByPrototype(StructureSpawn).find(s=>s.my) || null
        if (result){
            this.MY_Structure_Spawn = result
        }
    }
    private equal_arr(a:Array<any>,b:Array<any>){
        if (a.length != b.length){
            return false
        }
        for (let i = 0; i < a.length; i++) {
            if (a[i] != b[i] ){
                return false
            }
        }
        return true
    }



    /**
     * 统计房间中的兵源和希望的兵源数量
     */

    public checkCreep(target:army[]){
        let myCreep = getObjectsByPrototype(Creep).filter(item=>item.my)
        let temp:tempT = {}
        myCreep.forEach(Creep=>{
            let bodyType = Creep.body.map(CreepItem=>CreepItem.type)
            let index = target.findIndex(indexItem=>{
                return this.equal_arr(indexItem.type , bodyType)
            })
            if (index!=-1){
                if (!temp[index]){
                    temp[index]=1
                }else {
                    temp[index]= temp[index] + 1
                }
            }
        })
        for (const key in temp) {
            target[Number(key)].current = temp[key]
            if (target[Number(key)].current == target[Number(key)].plan_number) {
                target[Number(key)].is_success = true
            }
        }

    }
    /**
     * 更新基地能量值
     */
    public updateEnergy(){
        if (!this.MY_Structure_Spawn) {
            return
        }
        this._energy = this.MY_Structure_Spawn?.store.energy
    }
    /**
     * 查找固定资源位置
     * @constructor
     */
    public FindSource(){
        const source = getObjectsByPrototype(StructureContainer)
        return  source
    }

    /**
     * 房间中自己的小兵自卫
     */
    public selfDefense(){
        let myList = getObjectsByPrototype(Creep).filter(item=>item.my)
        for (let i =0;i<myList.length;i++){
            let item = myList[i]
            // @ts-ignore
            Boolean(item.defense) && item.defense()
        }
    }
}